package com.hope.androidproject;

import android.app.Activity;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.hope.androidproject.fragment.MySelfFragment;
import com.hope.androidproject.fragment.NewsFragment;
import com.hope.androidproject.fragment.VideoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private RadioGroup mRadioGroup;
    private Fragment currentFragment;
    private ViewPager mViewPager;
    private List<Fragment> fragments;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragments();
        initViews();
    }

    private void initFragments() {
        fragments = new ArrayList<>();
        fragments.add(new NewsFragment());
        fragments.add(new VideoFragment());
        fragments.add(new MySelfFragment());
    }

    private void initViews() {
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        mRadioGroup = (RadioGroup) findViewById(R.id.radio_container);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
            public int getCount() {
                return fragments.size();
            }

        });

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId)
                {
                    case R.id.news_radio:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.video_radio:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.myself_radio:
                        mViewPager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position)
                {
                    case 0:
                        mRadioGroup.check(R.id.news_radio);
                        break;
                    case 1:
                        mRadioGroup.check(R.id.video_radio);
                        break;
                    case 2:
                        mRadioGroup.check(R.id.myself_radio);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mRadioGroup.check(R.id.news_radio);
    }
}
